package com.suixun.util;

import org.apache.commons.lang.StringUtils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletRequest;
import java.text.*;
import java.util.*;

/**
 * 描述赶紧写上
 *
 * @author <a href="mailto:hellmash@gmail.com">hellmash</a>
 * @version create on 2015-05-02 11:37
 */
public class Utils {

    /**
     * 时间格式化工具
     */
    private final static Format dateFormat = new SimpleDateFormat("MMddHHmmssS");

    /** The FieldPosition. */
    private static final FieldPosition HELPER_POSITION = new FieldPosition(0);

    /**
     * 数字格式化
     * */
    private final static NumberFormat numberFormat = new DecimalFormat("0000");

    /**
     * 时间序列
     * */
    private static int seq = 0;

    private static final int MAX = 9999;

    /**
     * 生成校验码
     * @return
     */
    public static String genVerifyCode(){
        Random random=new Random();
        int first=random.nextInt(9)+1;
        String str=""+first;
        for(int i=0;i<5;i++){
            int digit=random.nextInt(10);
            str+=digit;
        }

        return str;
    }

   

    /**
     * 手机格式验证
     * @param tel
     * @return
     */
    public static boolean isTel(String tel){
        if(!tel.matches("^1[0-9][0-9]\\d{8}$")){
            return false;
        }
        return true;
    }

    /**
     * 通过uuid生成订单编号
     * @return
     */
    public static String getNextOrderSn(){
        String uuid = UUID.randomUUID().toString();
        return uuid.substring(0, 8)+uuid.substring(9,13)+uuid.substring(14,18)+uuid.substring(19,23)+uuid.substring(24);
    }

    /**
     * 时间格式生成序列,通过时间和末尾4个尾数
     * @return String
     */
    public static synchronized String generateSequenceNo() {

        Calendar rightNow = Calendar.getInstance();
        StringBuffer sb = new StringBuffer();
        dateFormat.format(rightNow.getTime(), sb, HELPER_POSITION);
        numberFormat.format(seq, sb, HELPER_POSITION);

        //序列累加，避免重复
        if (seq == MAX) {
            seq = 0;
        } else {
            seq++;
        }

        return sb.toString();
    }

    /**
     * 获取当前时间
     * @return
     */
    public static String getNow(){
        Calendar rightNow = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        StringBuffer sb = new StringBuffer();
        return  dateFormat.format(rightNow.getTime());
    }

    /**
     * 获取当前时间
     * @return
     */
    public static String getNowLongString(){
        Calendar rightNow = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        StringBuffer sb = new StringBuffer();
        return  dateFormat.format(rightNow.getTime());
    }

    public static String getParams(HttpServletRequest request) throws JsonProcessingException {  
        Map map = new HashMap();  
        Enumeration paramNames = request.getParameterNames();  
        while (paramNames.hasMoreElements()) {  
            String paramName = (String) paramNames.nextElement();  
  
            String[] paramValues = request.getParameterValues(paramName);  
            if (paramValues.length == 1) {  
                String paramValue = paramValues[0];  
                if (paramValue.length() != 0) {  
                    map.put(paramName, paramValue);  
                }  
            }  
        }  
  
        Set<Map.Entry<String, String>> set = map.entrySet();  
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(set);
    }  

}
